﻿using System;
using System.Web.UI;
using System.IO;
using System.Data;
using System.Xml;
using System.Text;
using QzEbook.AccessObject;
using QzEbook.Common;
using System.Collections.Generic;
using QzEbook.Admin;


namespace qzEbook.webUI
{
    public partial class GetConfig : System.Web.UI.Page
    {

        static int bookId;
        static string rawUrl;
        static int width;
        static int height;

        protected void Page_Load(object sender, EventArgs e)
        {
            rawUrl = Request.RawUrl;
            string bookIdString = Request["Bid"] as string;

            if (bookIdString == null)
            {
                Response.StatusCode = 404;
                Response.End();
            }

            int index = bookIdString.IndexOf("config.xml");
            if (index > -1)
            {
                bookIdString = bookIdString.Remove(index);
            }

            try
            {
                bookId = int.Parse(bookIdString);
            }
            catch
            {
                getImage(bookIdString);
                return;
            }
            Book book = new Book();
            book.BookId = bookId;
            book = book.select();

            //如果不私有与购买,那要判断用户;
            if (book.isSale || book.isSelf)
            {
                SessionUser userSession = Session[SessionManager.LOGINUSER] as SessionUser;

                ///只对私有加密.没有收费;
                if (userSession == null || userSession.Uid != book.EditorId)
                {
                    Response.Write("<div id=\"bookItem\"><div class=\"viewItem\"><strong><img src='images/siteIcon/alert.gif' alt='有问题' />此书你无权查看</strong></div></div>");
                    return;
                }

            }



            width = book.Width;
            height = book.Height;

            BookPage pageInfo = new BookPage();
            pageInfo.BookId = bookId;
            List<BookPage> pageList = pageInfo.selectList();

            StringBuilder sb = new StringBuilder();
            sb.Capacity = 2;

            Response.Clear();
            Response.ContentType = "text/xml";
            XmlTextWriter write = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

            write.WriteStartDocument();
            write.WriteStartElement("qz");
            write.WriteElementString("editor", book.EditorId.ToString());
            write.WriteElementString("title", book.Title);
            write.WriteElementString("width", book.Width.ToString());
            write.WriteElementString("height", book.Height.ToString());

            write.WriteStartElement("bgSound");
            write.WriteAttributeString("loop", book.BgSound.Loop.ToString());
            write.WriteString(book.BgSound.Path);
            write.WriteEndElement();

            write.WriteStartElement("bg");
            write.WriteAttributeString("repeat", book.Bg.Repeat.ToString());
            write.WriteAttributeString("alpha", book.Bg.Alpha.ToString());
            write.WriteString(book.Bg.Path);
            write.WriteEndElement();



            write.WriteStartElement("body");
            foreach (BookPage pages in pageList)
            {
                write.WriteStartElement("page");

                write.WriteElementString("contentindex", pages.Title);
                //string path = string.Format("{0}", pages.Path, 1000);

                write.WriteElementString("url", pages.Path);

                //直接生成由杂合型阅读器的缩略图;
                if (book.ViewType)
                {
                    book.Width = 140;
                    book.Height = 70;
                }
                string thumbPath = string.Format("{0}?w={1}|{2}", pages.Path, book.Width.ToString(), book.Height);
                write.WriteElementString("thumb", thumbPath);
                write.WriteEndElement();
            }
            write.WriteEndElement();
            write.WriteEndElement();
            write.WriteEndDocument();
            write.Flush();

            write.Close();
        }

        private void getImage(string urlpath)
        {
            string path = urlpath.Remove(0, bookId.ToString().Length);
            string[] pathParm = path.Split('?');
            path = pathParm[0];
            Response.ContentType = "image/jpeg";
            path = Server.MapPath(Config.UserBookSavePath + path);
            if (!File.Exists(path)) return;

            System.Drawing.Image image = System.Drawing.Image.FromFile(path);

            width = 2000;
            height = 1200;

            //小图;
            if (pathParm.Length > 1)
            {
                string[] widthHeight = pathParm[1].Split('=')[1].Split('|');
                width = int.Parse(widthHeight[0]);
                height = int.Parse(widthHeight[1]);
            }
            ImageGriphics imageGriphics = new ImageGriphics(image);
            imageGriphics.OutputSize = new System.Drawing.Size((int)width / 2, height);


            if (width/2 == height)
            {
                //这个是单p的缩略图;
                image = imageGriphics.GetThumb();
            }
            else
            {
                image = imageGriphics.GetNoCatThumb();
            }


            MemoryStream memory = new MemoryStream();
            image.Save(memory, System.Drawing.Imaging.ImageFormat.Jpeg);
            image.Dispose();
            Response.BinaryWrite(memory.GetBuffer());
            Response.End();
        }
    }
}